package com.sky.controller.admin;

import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping ("/admin/employee")
@Api (tags = "员工相关接口")
@Slf4j
@AllArgsConstructor
public class EmployeeController {
    private final EmployeeService employeeService;

    /**
     * 修改密码
     *
     * @param passwordEditDTO 条件
     * @return R
     */
    @ApiOperation (value = "修改密码")
    @PutMapping ("/editPassword")
    public Result updatePassword(@RequestBody PasswordEditDTO passwordEditDTO) {
        employeeService.updatePassword(passwordEditDTO);
        return Result.success();
    }

    /**
     * 启用/修改员工账号
     *
     * @param status 员工
     * @return R
     */
    @ApiOperation (value = "修改账号权限")
    @PostMapping ("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Integer id) {
        employeeService.updateStatus(status, id);
        return Result.success();
    }

    /**
     * 员工分页查询
     *
     * @param employeePageQueryDTO 分页条件
     * @return R
     */
    @ApiOperation (value = "员工分页查询")
    @GetMapping ("/page")
    public Result pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageBean pageBean = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 登录
     *
     * @param employeeLoginDTO 条件
     * @return Result
     */
    @ApiOperation (value = "用户登录")
    @PostMapping ("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录:{}", employeeLoginDTO);
        //调用service方法查找数据库
        return employeeService.login(employeeLoginDTO);
    }

    /**
     * 新增员工
     *
     * @param employee 信息
     * @return R
     */
    @ApiOperation (value = "新增员工")
    @PostMapping
    public Result add(@RequestBody Employee employee) {
        employeeService.add(employee);
        return Result.success();
    }

    /**
     * 根据id查询
     *
     * @param id 主键
     * @return R
     */
    @ApiOperation (value = "根据id查询")
    @GetMapping ("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Employee employee = employeeService.selectById(id);
        return Result.success(employee);
    }

    /**
     * 编辑员工信息
     *
     * @param employee 员工信息
     * @return R
     */
    @ApiOperation (value = "编辑员工信息")
    @PutMapping
    public Result updateEmployee(@RequestBody Employee employee) {
        employeeService.updateEmployee(employee);
        return Result.success();
    }

    @ApiOperation (value = "退出登录")
    @PostMapping ("/logout")
    public Result logout() {
        return Result.success();
    }
}
